package it.univaq.mancoosi.injectors.systemconfiguration.managers.ubuntu;

import it.univaq.mancoosi.injectors.systemconfiguration.managers.SGMLCatalogManager;
import it.univaq.mancoosi.mancoosimm.File;
import it.univaq.mancoosi.mancoosimm.SGMLDocument;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.emf.ecore.util.EcoreUtil;

public class UbuntuSGMLCatalogManager extends SGMLCatalogManager {
	
	protected int countDocs = 0;

	public UbuntuSGMLCatalogManager() {
		super();
	}

	public static SGMLCatalogManager getInstance() {
		if (INSTANCE == null)
			INSTANCE = new UbuntuSGMLCatalogManager();
		return INSTANCE;
	}	

	public Boolean exists(String sgmlDocumentName){
		return this.sgmlMap.containsKey(sgmlDocumentName);
	};

	@Override
	public void createSGMLCatalogFromSystem() {

		try {

			syncMap();

			if(UbuntuEnvironmentManager.getInstance().getEnvironment().getSgmlCatalog() != null){
				this.sgmlCatalog.getDocuments().addAll(UbuntuEnvironmentManager.getInstance().getEnvironment().getSgmlCatalog().getDocuments());
			}

			sgmlCatalog.setEnv(UbuntuEnvironmentManager.getInstance().getEnvironment());

			ArrayList<SGMLDocument> docs = new ArrayList<SGMLDocument>();


			java.io.File sgmlCatalogIndex = new java.io.File("/etc/sgml/catalog");
			if (sgmlCatalogIndex.exists()) {

				FileReader fileReader = new FileReader(sgmlCatalogIndex);
				BufferedReader fileBufferedReader = new BufferedReader(fileReader); 
				String line;

				while((line = fileBufferedReader.readLine()) != null){
					if (line.startsWith("CATALOG")) {
						String cat = line.split(" ")[1];
						java.io.File sgmlFile  = new java.io.File(cat); 
						
						String sgmlDocumentName = sgmlFile.getName().substring(0, sgmlFile.getName().length()-4);
						
						if(sgmlFile.exists()){
							countDocs++;
							if (!exists(sgmlDocumentName)) {
								SGMLDocument doc = factory.createSGMLDocument();
								doc.setLocation(UbuntuFileSystemManager.getInstance().createFile(sgmlFile.getParent()));
								doc.setDocument(UbuntuFileSystemManager.getInstance().createFile(sgmlFile.getPath()));
								doc.setName(sgmlDocumentName);
								this.sgmlMap.put(sgmlDocumentName, doc);
								docs.add(doc);
							}
						}
					}
				}

				sgmlCatalog.getDocuments().addAll(docs);
				UbuntuEnvironmentManager.getInstance().getEnvironment().setSgmlCatalog(sgmlCatalog);
			}

		} catch (Exception err) {
			err.printStackTrace();
		}   
	}

	@Override
	public void synchronize() {
		createSGMLCatalogFromSystem();
		
		if(this.sgmlMap.size() != countDocs){
			Iterator<SGMLDocument> iterator = this.sgmlMap.values().iterator();
			while (iterator.hasNext()) {
				SGMLDocument curr = iterator.next();
				if (!existsInTheSystem(curr.getDocument())) {
					this.sgmlMap.remove(curr);
					EcoreUtil.delete(curr);
					UbuntuFileSystemManager.getInstance().deleteFile(curr.getDocument());
				}
			}
		}
	}
	
	private boolean existsInTheSystem (File doc) {
		try {
			BufferedReader filebuf = 
				new BufferedReader(new FileReader("/etc/sgml/catalog"));
			String nextStr;
			nextStr = filebuf.readLine();
			while (nextStr != null){
				if (nextStr.contains(doc.getName()) &&
						!doc.isIsMissing())
					return true;
				else nextStr = filebuf.readLine();
			} 
		}
		catch (Exception e) {
			e.printStackTrace();
		}

		return false;
	}

}

